package aad.storyboard.model;

import java.util.ArrayList;
import java.util.HashMap;

import utils.ViewUtils;

import aad.storyboard.controller.GUIComposer;
import aad.storyboard.controller.SceneTransition;
import aad.storyboard.controller.TransitionProvider;
import aad.storyboard.view.GraphicView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;

public class ColorFadeAnimation extends Animation{
	
	private static Paint mForeGroundPaint;  
    private static Paint mBackGroundPaint;   
       
    private static int foregrndalpha =255;  
    private static int mAlpha = 0;   
   
    private HashMap<Integer, Fader> fadeStrategies;
    private Fader fader;
    
    private static Bitmap inextCharacter;  
    private static ArrayList <Image> icurrentCharacters;   
    private ColorMatrix cm;
   
    public ColorFadeAnimation(int startTime, int duration, int fadeTargetColor) 
    {  
    	super(startTime,duration);   	
    	mForeGroundPaint = new Paint();  
    	mBackGroundPaint = new Paint(); 
    	
    	cm = new ColorMatrix();   
    	
    	inextCharacter=TransitionProvider.getInstance().getFadeBitmap();
    	
     fadeStrategies=TransitionProvider.getInstance().getFadeStratiegies();
    
     this.fader=fadeStrategies.get(fadeTargetColor);
      
     }  
      
    
	@Override
	public void animate() {
		icurrentCharacters=((GraphicView) (GUIComposer.getInstance().getGraphicalView())).getImages();
    	
    	if (icurrentCharacters!=null)
    	{
    		
    		for (Image img: icurrentCharacters)
       	{
       		img.setPaint(mForeGroundPaint);
       		
       	} 
         
    		if(foregrndalpha > 0 )  
    		{                 
    			foregrndalpha -= 5;  
    			mAlpha+=5;
    			mForeGroundPaint.setAlpha(foregrndalpha); 
          
    			fader.fade(cm, mAlpha);
    			mBackGroundPaint.setColorFilter(new ColorMatrixColorFilter(cm));       
         
    			((GraphicView)(GUIComposer.getInstance().getGraphicalView())).setFadeTarget(inextCharacter, mBackGroundPaint);         
         
        
    		}
    		
       	}
       		
	}

	@Override
	public void adjustParameters() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void undo() {
		System.out.println("Undoing");
		mForeGroundPaint.setAlpha(255);		
		if (icurrentCharacters!=null)
    	{
    		
    		for (Image img: icurrentCharacters)
       	{
       		img.setPaint(mForeGroundPaint);
       		
       	}
    	((GraphicView)(GUIComposer.getInstance().getGraphicalView())).setFadeTarget(null, null);
		
    	}
	}
	     

}

